//******************************************************************************
//
// ATM系统 -  Withdraw.java 
// 参考了 http://www.cs.gordon.edu/courses/cs211/ATMExample/index.html
// 
//******************************************************************************

package banking;

import java.util.Date;

import atm.ATM;
import atm.Session;

/**
 * 交易子类 用于处理存取款的业务
 * @author 莫东林
 * @version 2018/11/16
 *
 */
public class MoneyPump extends Transaction {
	
	public static final int TRANS_UNSTART = 1; // 未开始
	public static final int TRANS_GETDATA = 2; // 获取交易请求
	public static final int TRANS_SUCCESS = 3; // 交易成功
	public static final int TRANS_FAILURE = 4; // 交易失败
	public static final int TRANS_EXIT = 4; // 退出
	
	
	public MoneyPump(Session session, Account acct, int type) {
		super(session,acct,type);
		ATM machine = ATM.getInstance();
		// 当选择取款交易时,需要改变显示屏的显示,需要改变数字键盘的状态
		if(type == TYPE_WITHDRAW){
			machine.getDisplay().setText("请输入取款金额");
			//开启键盘多输入
			machine.getDigitButton().enterShowMultiInput("WithdrawInfoServlet");
		}
		// 存款交易
		else if(type == TYPE_DEPOSIT){
			machine.getDisplay().setText("请将整理好的钞票放入入钞口内（只支持面值为100元的纸币）");
			//键盘禁止输入
			machine.getDigitButton().enterForbidInput("DepositInfoServlet");
			//开启存款口
			machine.getMoneyBox().enterPutMoneyState();
		}
		//转账交易
		else if(type == TYPE_TRANSFER){
			machine.getDisplay().setText("请输入转入卡号");
			//开启键盘
			machine.getDigitButton().enterShowMultiInput("CheckTransServlet");
		}
		//查询余额
		else if(type == TYPE_BALANCE){
			double balance = ATM.getInstance().getSession().getAccount().getAmount(ATM.getInstance().getSession().getAccount().getCard_no());
			machine.getDisplay().setText("您的余额为" + balance + "元（按任意键返回）");
			//开启键盘
			machine.getDigitButton().enterSingleInput("MenuServlet");
		}

	}
	
	/**
	 * 从用户账户扣取金额
	 */
	public void getMoney() {
		int ret = this.getAccount().withdraw(this.getAmount());
		ATM machine =  ATM.getInstance();
		Trade trade = null;
		// 扣取成功
		if(ret == 0) {
			//设置交易记录状态
			trade = new Trade(getAccount().getCard_no(), 0, getAmount(),0);
			// 显示屏更新 数字键盘状态更新 
			this.setState(TRANS_SUCCESS);
			machine.getDisplay().setText("请在出款口取走您的现金。你的余额是"+this.getAccount().getBalance()+"<br>"+"打印:0 不打印:1");
			machine.getMoneyBox().enterGetMoneyState(this.getAmount());
			machine.getDigitButton().enterSingleInput("WithdrawPrintServlet");
		}
		// 余额不足
		else if(ret == 1){
			//设置交易记录状态
			trade = new Trade(getAccount().getCard_no(), 0, getAmount(),1);
			this.setState(TRANS_FAILURE);
			machine.getDisplay().setText("余额不足，取款失败 退卡:5 返回:9");
			machine.getDigitButton().enterSingleInput("BackServlet");
		}
		//未知错误
		else if(ret == 2){
			
		}
		//保存交易记录
		trade.saveTrade(trade);
	}
	
	/**
	 * 存款
	 */
	public void putMoney(){
		int ret = this.getAccount().deposit(this.getAmount());
		ATM machine =  ATM.getInstance();
		Trade trade = null;
		//成功
		if(ret == 0){
			//设置交易记录状态
			trade = new Trade(getAccount().getCard_no(), 1, getAmount(),0);
			// 显示屏更新 数字键盘状态更新 
			this.setState(TRANS_SUCCESS);
			machine.getDisplay().setText("存款成功。你的余额是"+this.getAccount().getBalance()+"<br>"+"打印:0 不打印:1");
			machine.getDigitButton().enterSingleInput("DepositPrintServlet");
		}
		//失败
		else if(ret == 1){
			//设置交易记录状态
			trade = new Trade(getAccount().getCard_no(), 1, getAmount(),1);
		}
		trade.saveTrade(trade);
	}
	
	/**
	 * 转账
	 */
	public void transMoney() {
		//扣款
		System.out.println(this);
		int ret = this.getAccount().transMoney(this.getAmount(), this.getCard_no());
		ATM machine =  ATM.getInstance();
		Trade trade = null;
		// 转账成功
		if(ret == 0) {
			//设置交易记录状态
			trade = new Trade(getAccount().getCard_no(), getCard_no(), getAmount(),0);
			// 显示屏更新 数字键盘状态更新 
			this.setState(TRANS_SUCCESS);
			machine.getDisplay().setText("转账成功。你的余额是"+this.getAccount().getBalance()+"<br>"+"打印:0 不打印:1");
			//打印
			machine.getDigitButton().enterSingleInput("TransPrintServlet");
		}
		// 余额不足
		else if(ret == 1){
			//设置交易记录状态
			trade = new Trade(getAccount().getCard_no(), getCard_no(), getAmount(),1);
			this.setState(TRANS_FAILURE);
			machine.getDisplay().setText("余额不足，转账失败 退卡:5 返回:9");
			machine.getDigitButton().enterSingleInput("BackServlet");
		}
		//未知错误
		else if(ret == 2){
			
		}
		//保存交易记录
		trade.saveTrade(trade);
	}
	
	/**
	 * 处理取款打印
	 * @param flag 0:打印 1:不打印
	 */
	public void withdrawPrint(int flag) {
		switch(flag){
		//打印
		case 0:
			ATM.getInstance().getReceipt().setText(getAccount().getCard_no(), 
					ATM.getInstance().getSession().getTransaction().getAccount().getCard_no()
					, ATM.getInstance().getSession().getTransaction().getType(), 
					ATM.getInstance().getSession().getTransaction().getAmount(), 
					new Date());
			break;
		//不打印
		case 1:
			ATM.getInstance().getReceipt().setText("");
			break;
		}
		// 显示屏更新 数字键盘状态更新 
		ATM.getInstance().getReceipt().setText(ATM.getInstance().getReceipt().getText().replace("\n", "<br>"));
		this.setState(TRANS_EXIT);
		this.getSession().setState(Session.CHOOSING);
		ATM.getInstance().enterMenu();
	}
	
	/**
	 * 处理存款打印
	 * @param flag 0:打印 1:不打印
	 */
	public void depositPrint(int flag) {
		switch(flag){
		//打印
		case 0:
			ATM.getInstance().getReceipt().setText(getAccount().getCard_no(), 
					ATM.getInstance().getSession().getTransaction().getAccount().getCard_no()
					, ATM.getInstance().getSession().getTransaction().getType(), 
					ATM.getInstance().getSession().getTransaction().getAmount(), 
					new Date());
			break;
		//不打印
		case 1:
			ATM.getInstance().getReceipt().setText("");
			break;
		}
		// 显示屏更新 数字键盘状态更新 
		ATM.getInstance().getReceipt().setText(ATM.getInstance().getReceipt().getText().replace("\n", "<br>"));
		this.setState(TRANS_EXIT);
		this.getSession().setState(Session.CHOOSING);
		ATM.getInstance().enterMenu();
	}
	
	/**
	 * 处理转账打印
	 * @param flag 0:打印 1:不打印
	 */
	public void transPrint(int flag) {
		switch(flag){
		//打印
		case 0:
			ATM.getInstance().getReceipt().setText(
					//操作卡号
					getAccount().getCard_no(),
					//乙方卡号
					ATM.getInstance().getSession().getTransaction().getCard_no(),
					ATM.getInstance().getSession().getTransaction().getType(), 
					ATM.getInstance().getSession().getTransaction().getAmount(), 
					new Date());
			break;
		//不打印
		case 1:
			ATM.getInstance().getReceipt().setText("");
			break;
		}
		// 显示屏更新 数字键盘状态更新 
		ATM.getInstance().getReceipt().setText(ATM.getInstance().getReceipt().getText().replace("\n", "<br>"));
		this.setState(TRANS_EXIT);
		this.getSession().setState(Session.CHOOSING);
		ATM.getInstance().enterMenu();
	}
	
}
